package com.example.idachuappone.index.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.index.entity.Comment;
import com.example.idachuappone.utils.ComUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class CommentAdapter extends BaseAdapter {
	private BitmapUtils bu;
	private Context context;
	ViewHolder holder;
	private List<Comment> list;
	private String title;

	private class ViewHolder {
		GridView gv;
		ImageView img_degree;
		ImageView img_star;
		LinearLayout ll_gv_isvis;
		TextView tv_comment;
		TextView tv_name;
		TextView tv_time;

		private ViewHolder() {
		}
	}

	public CommentAdapter(Context context, BitmapUtils bu) {
		this.holder = null;
		this.context = context;
		this.bu = bu;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setList(List<Comment> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list == null ? 0 : this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (Comment) this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LinearLayout.inflate(this.context, R.layout.simple_comment_item, null);
			this.holder = new ViewHolder();
			this.holder.gv = (GridView) convertView.findViewById(R.id.gv_list);
			this.holder.img_star = (ImageView) convertView.findViewById(R.id.img_star);
			this.holder.img_degree = (ImageView) convertView.findViewById(R.id.img_degree);
			this.holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			this.holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			this.holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
			this.holder.ll_gv_isvis = (LinearLayout) convertView.findViewById(R.id.ll_gv_isvis);
			convertView.setTag(this.holder);
		} else {
			this.holder = (ViewHolder) convertView.getTag();
		}
		Comment comment = this.list.get(position);
		if (comment.getUser() == null || comment.getUser().getName() == null
				|| comment.getUser().getName().trim().length() <= 0) {
			this.holder.tv_name.setText("\u533f\u540d");
			this.holder.img_degree.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		} else {
			this.holder.tv_name.setText(comment.getUser().getName());
			if (comment.getUser().getDegree() == 0) {
				this.holder.img_degree.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			} else if (comment.getUser().getDegree() == 1) {
				this.holder.img_degree.setVisibility(0);
				this.holder.img_degree.setImageResource(R.drawable.name_experience_vip);
			} else if (comment.getUser().getDegree() == 2) {
				this.holder.img_degree.setVisibility(0);
				this.holder.img_degree.setImageResource(R.drawable.name_general_vip);
			} else if (comment.getUser().getDegree() == 3) {
				this.holder.img_degree.setVisibility(0);
				this.holder.img_degree.setImageResource(R.drawable.name_sliver_vip);
			} else if (comment.getUser().getDegree() == 4) {
				this.holder.img_degree.setVisibility(0);
				this.holder.img_degree.setImageResource(R.drawable.name_god_vip);
			}
		}
		if (Double.valueOf(comment.getStar()).doubleValue() > 4.0d) {
			this.holder.img_star.setImageResource(R.drawable.icon_star05);
		} else if (Double.valueOf(comment.getStar()).doubleValue() > 3.0d) {
			this.holder.img_star.setImageResource(R.drawable.icon_star04);
		} else if (Double.valueOf(comment.getStar()).doubleValue() > 2.0d) {
			this.holder.img_star.setImageResource(R.drawable.icon_star03);
		} else if (Double.valueOf(comment.getStar()).doubleValue() > 1.0d) {
			this.holder.img_star.setImageResource(R.drawable.icon_star02);
		} else {
			this.holder.img_star.setImageResource(R.drawable.icon_star01);
		}
		this.holder.tv_time.setText(ComUtil.FormatCalenderTime2(comment.getDateline()));
		this.holder.tv_comment.setText(comment.getContent());
		if (comment.getImgs() == null || comment.getImgs().size() <= 0) {
			this.holder.ll_gv_isvis.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		} else {
			this.holder.ll_gv_isvis.setVisibility(0);
			this.holder.ll_gv_isvis.setFocusable(false);
			CommentImgAdapter adapter = new CommentImgAdapter(this.context, this.bu);
			adapter.setList(comment.getImgs());
			adapter.setTitle(this.title);
			this.holder.gv.setAdapter(adapter);
		}
		return convertView;
	}
}
